package com.lg.partition27;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/31 9:30
 */
public class P2656采蘑菇 {

    private static final int N = 80010, M = 400010;
    private static int[] h = new int[N], hc = new int[N], e = new int[M], ne = new int[M], w = new int[M], hcw = new int[N];
    private static double[] f = new double[N];
    private static int[] stk = new int[N], dfn = new int[N], low = new int[N], scc = new int[N];
    private static boolean[] st = new boolean[N];
    private static int[] q = new int[N], dist = new int[N];
    private static int n, m, idx = 0, timestamp = 0, top = 0, sccId = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        for (int i = 1; i < N; i++) h[i] = hc[i] = -1;
        for (int i = 1; i <= m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            double x = Double.parseDouble(ss[3]);
            add(a, b, c, x);
        }
        int s = Integer.parseInt(reader.readLine());
        writer.write(resolve(s) + "\n");
        writer.flush();
    }

    private static int resolve(int s) {
        //tarjan处理出强连通分量，并缩点
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == 0) tarjan(i);
        }
        for (int u = 1; u <= n; u++) {
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                //不在一个scc中，这条边就只能走一次，创建带权边；否则在同一个scc中，将这条边对点权的贡献加上
                if (scc[u] != scc[v]) add(scc[u], scc[v], w[i]);
                else hcw[scc[v]] += cal(w[i], f[i]);
            }
        }
        //在新图上跑一遍spfa求最长路
        return spfa(s);
    }

    private static int spfa(int s) {
        Arrays.fill(st, false);
        int hh = 0, tt = 0;
        q[0] = scc[s];
        dist[scc[s]] = hcw[scc[s]];
        while (hh <= tt) {
            int u = q[hh++];
            st[u] = false;
            for (int i = hc[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (dist[v] < dist[u] + hcw[v] + w[i]) {
                    dist[v] = dist[u] + hcw[v] + w[i];
                    if (st[v]) continue;
                    q[++tt] = v;
                    st[v] = true;
                }
            }
        }
        int ans = 0;
        for (int i = 1; i <= sccId; i++) ans = Math.max(ans, dist[i]);
        return ans;
    }

    private static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stk[++top] = u;
        st[u] = true;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v);
                low[u] = Math.min(low[u], low[v]);
            } else if (st[v]) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            sccId++;
            int v = -1;
            do {
                v = stk[top--];
                st[v] = false;
                scc[v] = sccId;
            } while (u != v);
        }
    }

    private static void add(int a, int b, int c, double x) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        f[idx] = x;
        w[idx++] = c;
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = hc[a];
        hc[a] = idx;
        w[idx++] = c;
    }

    private static int cal(int a, double x) {
        int res = a;
        while (a != 0) {
            res += a * x;
            a *= x;
        }
        return res;
    }
}
